package com.learn.kotlinlearndemolist.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.databinding.DataBindingUtil.inflate
import androidx.fragment.app.Fragment
import androidx.navigation.NavController
import androidx.navigation.Navigation
import com.learn.kotlinlearndemolist.R
import com.learn.kotlinlearndemolist.coroutinesdemo.testChannel
import com.learn.kotlinlearndemolist.coroutinesdemo.testDivide
import com.learn.kotlinlearndemolist.coroutinesdemo.testDivideLine
import com.learn.kotlinlearndemolist.coroutinesdemo.testEighth
import com.learn.kotlinlearndemolist.coroutinesdemo.testEleventh
import com.learn.kotlinlearndemolist.coroutinesdemo.testFifth
import com.learn.kotlinlearndemolist.coroutinesdemo.testFirst
import com.learn.kotlinlearndemolist.coroutinesdemo.testFourteenth
import com.learn.kotlinlearndemolist.coroutinesdemo.testFourth
import com.learn.kotlinlearndemolist.coroutinesdemo.testNinth
import com.learn.kotlinlearndemolist.coroutinesdemo.testPip
import com.learn.kotlinlearndemolist.coroutinesdemo.testPip2
import com.learn.kotlinlearndemolist.coroutinesdemo.testPrev
import com.learn.kotlinlearndemolist.coroutinesdemo.testRepeat
import com.learn.kotlinlearndemolist.coroutinesdemo.testSecond
import com.learn.kotlinlearndemolist.coroutinesdemo.testSeventh
import com.learn.kotlinlearndemolist.coroutinesdemo.testShared
import com.learn.kotlinlearndemolist.coroutinesdemo.testSixth
import com.learn.kotlinlearndemolist.coroutinesdemo.testTenth
import com.learn.kotlinlearndemolist.coroutinesdemo.testThird
import com.learn.kotlinlearndemolist.coroutinesdemo.testThirteen
import com.learn.kotlinlearndemolist.coroutinesdemo.testTwelve
import com.learn.kotlinlearndemolist.databinding.FragmentFirstBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class FirstFragment : Fragment() {
    private lateinit var mBinding: FragmentFirstBinding
    private lateinit var navController: NavController

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        // Inflate the layout for this fragment
        mBinding = inflate(
            LayoutInflater.from(requireContext()),
            R.layout.fragment_first,
            container,
            false
        )
        return mBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
//        val textView: TextView = findViewById(R.id.text_view)
        //判空
        val charSequence = mBinding.textView.text ?: "1";
        mBinding.textView.setOnClickListener {
            Toast.makeText(requireContext(), charSequence, Toast.LENGTH_SHORT).show()
        }
        mBinding.topView.setOnItemSelectedListener { item ->
            navController = Navigation.findNavController(mBinding.topView)
            when (item.itemId) {
                R.id.firstFragment ->
                    navController.navigate(R.id.action_mainFragment_to_thirdFragment)
                R.id.secondFragment ->
                    navController.navigate(R.id.action_mainFragment_to_fourthFragment)
                else -> null
            }
            true
        }
        mBinding.back.setOnClickListener {
//            navController.navigateUp()
        }
        mBinding.cBtn.setOnClickListener {
            testCoroutines()
            testDivideLine()
            testFourth()
            testDivideLine()
        }
        mBinding.cChannelBtn.setOnClickListener {
            testChannel()
            testFifth()
            testSixth()
            testSeventh()
            testEighth()
            testNinth()
            testTenth()
            testEleventh()
            testTwelve()
            testThirteen()
            testFourteenth()
        }
        mBinding.cBtn1.setOnClickListener {
            testPip()
            testPip2()
            testShared()
        }
    }

    private fun testCoroutines() {
        testDivide()
        testPrev()
        testDivide()
        testFirst()
        testDivide()
        testSecond()
        testDivide()
        testThird()
        testDivide()
    }
}